# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2020 Intel Corporation

FROM ubuntu:18.04 as builder

USER root
WORKDIR /

# Creating user openvino and adding it to groups "video" and "users" to use GPU and VPU
RUN useradd -ms /bin/bash -G video,users openvino && \
    chown openvino -R /home/openvino

# hadolint ignore=DL3008
RUN apt-get update && apt upgrade -y --no-install-recommends && \
    apt-get install -y --no-install-recommends curl && \
    rm -rf /var/lib/apt/lists/*

RUN ln -snf /usr/share/zoneinfo/$(curl https://ipapi.co/timezone -k) /etc/localtime

ARG DEPENDENCIES="autoconf \
                  automake \
                  build-essential \
                  libgtk-3-0 \
                  libcairo2-dev \
                  gobject-introspection \
                  libglib2.0-0 \
                  libgdk-pixbuf2.0-0 \
                  cmake \
                  cpio \
                  sudo \
                  libtool \
                  udev \
                  unzip \
                  kmod \
                  wget \
                  libgstreamer1.0-0 \
                  gstreamer1.0-plugins-base \
                  gstreamer1.0-plugins-good \
                  gstreamer1.0-plugins-bad \
                  gstreamer1.0-vaapi \
                  ffmpeg \
                  dos2unix"

# hadolint ignore=DL3008
RUN apt-get update && \
    apt-get install -y --no-install-recommends ${DEPENDENCIES} && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /thirdparty
RUN sed -Ei 's/# deb-src /deb-src /' /etc/apt/sources.list && \
    apt-get update && \
    apt-get source ${DEPENDENCIES} && \
    rm -rf /var/lib/apt/lists/*


# setup Python
ENV PYTHON_VER python3.6

# hadolint ignore=DL3008
RUN apt-get update && \
    apt-get install -y --no-install-recommends python3-pip python3-dev python3-setuptools lib${PYTHON_VER} && \
    rm -rf /var/lib/apt/lists/*

RUN ${PYTHON_VER} -m pip install --upgrade pip

# get product from URL
ARG package_url=http://registrationcenter-download.intel.com/akdlm/irc_nas/16612/l_openvino_toolkit_p_2020.2.120.tgz
ARG TEMP_DIR=/tmp/openvino_installer

WORKDIR ${TEMP_DIR}
# hadolint ignore=DL3020
RUN wget ${package_url}

# install product by copying archive content
ARG TEMP_DIR=/tmp/openvino_installer
ENV INTEL_OPENVINO_DIR /opt/intel/openvino


RUN tar -xzf "${TEMP_DIR}"/*.tgz && \
    cd l_openvino_toolkit* && \
    sed -i 's/decline/accept/g' silent.cfg && \
    ./install.sh -s silent.cfg

RUN export OV_BUILD OV_FOLDER
RUN OV_BUILD="$(find . -maxdepth 1 -type d -name "*openvino*" | grep -oP '(?<=_)\d+.\d+.\d+')" && \
    OV_YEAR="$(find . -maxdepth 1 -type d -name "*openvino*" | grep -oP '(?<=_)\d+')" && \
    OV_FOLDER="$(find . -maxdepth 1 -type d -name "*openvino*")" && \
    ln --symbolic /opt/intel/openvino_"$OV_BUILD"/ /opt/intel/openvino 

FROM ubuntu:18.04

WORKDIR /tmp

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        gcc \
        make \
        sudo \
        libusb-1.0-0 \
        libboost-filesystem1.65-dev=1.65.1+dfsg-0ubuntu5 \
        libboost-thread1.65-dev=1.65.1+dfsg-0ubuntu5 \
        libjson-c3=0.12.1-1.3 libxxf86vm-dev=1:1.1.4-1 \
        libboost-program-options1.65 \
        lsb-release && \      
    rm -rf /var/lib/apt/lists/*

COPY --from=builder /opt/intel/openvino/python /opt/intel/openvino/python
COPY --from=builder /opt/intel/openvino/bin /opt/intel/openvino/bin
COPY --from=builder /opt/intel/openvino/deployment_tools /opt/intel/openvino/deployment_tools

RUN sudo sed -i '/"abort_if_hw_reset_failed"/ s/true/false/' /opt/intel/openvino/deployment_tools/inference_engine/external/hddl/config/hddl_autoboot.config
RUN sudo sed -i '/"device_snapshot_mode"/ s/none/full/' /opt/intel/openvino/deployment_tools/inference_engine/external/hddl/config/hddl_service.config
COPY run_hddl.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/run_hddl.sh

